﻿// -----------------------------------------------------------------------
// <copyright file="HolidayData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Holiday
    /// </summary>
    public class HolidayData
    {
        /// <summary>
        /// Get Holiday By Id
        /// </summary>
        /// <param name="holidayId">Param holidayId</param>
        /// <returns>Return Holiday</returns>
        public HolidayEN GetHolidayById(int holidayId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                HolidayEN holiday = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneHolidayGetById, holidayId);
                while (reader.Read())
                {
                    holiday = new HolidayEN();

                    if (!reader.IsDBNull(0))
                    {
                        holiday.HolidayId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        holiday.DateId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                       holiday.Descriptions = reader.GetString(2);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return holiday;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Holiday
        /// </summary>
        /// <returns>Return list all Holiday</returns>
        public IList<HolidayEN> GetHolidays()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<HolidayEN> lstHoliday = new List<HolidayEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneHolidayGetAll);
                while (reader.Read())
                {
                    HolidayEN holiday = new HolidayEN();

                    if (!reader.IsDBNull(0))
                    {
                        holiday.HolidayId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        holiday.DateId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        holiday.Descriptions = reader.GetString(2);
                    }

                    lstHoliday.Add(holiday);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstHoliday;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Holiday
        /// </summary>
        /// <param name="holiday">Paarm Holiday</param>
        /// <returns>Insert Holiday</returns>
        public int Insert(HolidayEN holiday)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Holiday.DateId, holiday.DateId),
                                    new SqlParameter(Constants.SqlParamerterName.Holiday.Descriptions, holiday.Descriptions)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneHolidayInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Holiday
        /// </summary>
        /// <param name="holiday">Param Holiday</param>
        public void Update(HolidayEN holiday)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Holiday.HolidayId, holiday.HolidayId),
                                    new SqlParameter(Constants.SqlParamerterName.Holiday.DateId, holiday.DateId),
                                    new SqlParameter(Constants.SqlParamerterName.Holiday.Descriptions, holiday.Descriptions)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneHolidayUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Holiday by holidayId
        /// </summary>
        /// <param name="holidayId">param holidayId</param>
        public void Delete(int holidayId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Holiday.HolidayId, holidayId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneHolidayDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Check exist DateId
        /// </summary>
        /// <param name="id">Param id</param>
        /// <param name="dateId">Param dateId</param>
        /// <returns>Return true or false</returns>
        public bool Exist(int id, int dateId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneHolidayExist, id, dateId));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Determines whether the specified date time is holiday.
        /// </summary>
        /// <param name="dateTime">The date time.</param>
        /// <returns>return bool</returns>
        public bool IsHoliday(DateTime dateTime)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneIsHoliday, dateTime));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}